﻿/**
* ...
* @author alijayameilio
*/

package particling.emitter;

import flash.events.Event;
import flash.events.EventDispatcher;
import particling.event.ParticleEvent;
import particling.ParticleConfig;
import particling.Particle;
import particling.force.Force;
import particling.AParticle;
import particling.ShapeConfig;

class ParticleEmitter extends EventDispatcher
{

	public var x:Float;
	public var y:Float;
	public var particle:Particle;
	public var configList:Array<ParticleConfig>;
	public var forceList:Array<Force>;
	public var numberCreated:Int;
	
	private function new(__particle:Particle) :Void
	{
		super();
		particle = __particle;
		x = 0;
		y = 0;
		configList = new Array<ParticleConfig>();
		forceList = new Array<Force>();
		numberCreated = 0;
	}
	
	public function releaseParticle(amount:Int) :Void
	{
		var indexConfig:Int = 0;
		while (true)
		{
			if (indexConfig == configList.length)
			{
				break;
			}
			var config:ParticleConfig = configList[indexConfig];
			var curParticleAmount:Int = Math.round(config.percent / 100 * amount);
			var hasMade:Int = 0;
			while (true)
			{
				if (hasMade == curParticleAmount)
				{
					break;
				}
				var lifeTime:Int;
				switch(config.lifeTime)
				{
					case INFINITE:
					lifeTime = -1;
					case RANGE(min, max):
					lifeTime = Std.random(max - min) + min;
					case ABSOLUTE(value):
					lifeTime = value;
				}
				var selectedParticle:AParticle = new AParticle(config.shapeConfig, this, lifeTime, config.rotationFollow);
				particle.particles.push(selectedParticle);
				particle.container.addChild(selectedParticle.container);
				calculateXYPos(selectedParticle);
				dispatchEvent(new ParticleEvent(ParticleEvent.onCreate, selectedParticle));
				hasMade++;
				numberCreated++;
				particle.curAmount++;
			}
			indexConfig++;
		}
	}
	
	public function calculateXYPos(__selectedParticle:AParticle):Void
	{
		
	}
	
	public function killParticle(__aParticle:AParticle)
	{
		particle.curAmount--;
		__aParticle.container.parent.removeChild(__aParticle.container);
		particle.particles.remove(__aParticle);
		dispatchEvent(new ParticleEvent(ParticleEvent.onDie, __aParticle));
	}
	
}